project = cipher
TOP=$(shell pwd)
BUILD = $(TOP)/build
BIN = $(BUILD)/bin
LIB = $(BUILD)/lib
OBJ = $(BUILD)/obj
TEST = $(BIN)/test

SRCS = $(wildcard $(TOP)/pkcs/*.c)
SRCS += $(wildcard $(TOP)/alg/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
OBJS_NODIR = $(addprefix $(OBJ)/,$(notdir $(OBJS)))

TEST_SRC = $(wildcard $(TOP)/test/*.c)
TEST_OBJ = $(patsubst %.c,%,$(TEST_SRC))
TEST_BIN = $(addprefix $(TEST)/,$(notdir $(TEST_OBJ)))

VPATH = $(TOP)/alg $(TOP)/pkcs $(TOP)/test
INC = -I$(TOP)/include/
LDLIB := -L$(LIB) -Wl,-rpath=$(LIB) -l$(project)
CFLAG = -fPIC -g

all: $(project) test

prebuild:
	mkdir -pv $(BUILD)
	mkdir -pv $(BIN)
	mkdir -pv $(LIB)
	mkdir -pv $(OBJ)
	mkdir -pv $(TEST)

$(project):prebuild $(OBJS_NODIR)
	$(CC) -shared $(OBJS_NODIR) -o $(LIB)/lib$@.so

$(OBJ)/%.o : %.c
	$(CC) -o $@ -c $< $(INC) $(CFLAG)

test: $(project) $(TEST_BIN)


$(TEST)/% : %.c
	$(CC) $< -o $@  $(INC) $(LDLIB) $(CFLAG)

clean:
	rm -rf $(BUILD)



